package com.eastfair.auth.company;

import cn.hutool.core.date.LocalDateTimeUtil;
import com.eastfair.auth.entity.UserAccount;
import com.eastfair.auth.service.UserAccountService;
import com.eastfair.cache.gateway.LoginCacheKeyBuilder;
import com.eastfair.cache.repository.CacheOps;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKey;
import com.eastfair.gateway.api.BackListServiceFeign;
import com.eastfair.gateway.dto.BackListDTO;
import com.eastfair.log.point.PointUtil;
import com.eastfair.point.business.auth.BusinUpdUserPoint;
import com.eastfair.point.business.gateway.BusinBalckPoint;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 登陆事件
 * ligang
 */
@Component
@Slf4j
public class LoginHandler {
    @Autowired
    private CacheOps cacheOps;
    //    @Autowired
//    PointUtil pointUtil;
    @Autowired
    BackListServiceFeign backListServiceFeign;

//    @Value("${com.eastfair.firstLoginFailNum}")
//    private Integer firstLoginFailNum;
//
//    @Value("${com.eastfair.secondLoginFailNum}")
//    private Integer secondLoginFailNum;

    @Autowired
    UserAccountService userAccountService;

    /**
     * 登陆成功操作
     */
    public void executeLoginSucc(Long userId) {
        //删除当天登陆失败次数
        CacheKey key = new LoginCacheKeyBuilder().key(queryUserKey(userId));
        cacheOps.del(key);
    }

    /**
     * 登陆失败操作
     */
    public void executeLoginFail(Long userId) {
        //添加当天登陆失败次数
        CacheKey key = new LoginCacheKeyBuilder().key(queryUserKey(userId));
        Long i = cacheOps.incr(key);
        log.info("登录失败次数 is {},用户id is {}", i, queryUserKey(userId));
//        if (firstLoginFailNum == i.intValue()) {
//            //登陆3次,禁止登陆10分钟
////            BusinBalckPoint businBalckPoint = new BusinBalckPoint();
////            businBalckPoint.setUserId(userId);
////            businBalckPoint.setStartTime(LocalDateTimeUtil.now());
////            businBalckPoint.setEndTime(LocalDateTimeUtil.now().plusMinutes(10));
////            pointUtil.addBusinPointMsg(businBalckPoint);
//            BackListDTO backListDTO = new BackListDTO();
//            backListDTO.setUserId(userId);
//            backListDTO.setStartTime(LocalDateTimeUtil.now());
//            backListDTO.setEndTime(LocalDateTimeUtil.now().plusMinutes(10));
//            backListServiceFeign.executeAddBackList(backListDTO);
//        } else if (secondLoginFailNum <= i.intValue()) {
//            //登陆5次，账号锁定
////            BusinUpdUserPoint businUpdUserPoint2 = new BusinUpdUserPoint();
////            businUpdUserPoint2.setUserId(userId);
////            businUpdUserPoint2.setIsEnabled(BusinessConstant.NO);
////            pointUtil.addBusinPointMsg(businUpdUserPoint2);
//            UserAccount userAccount = userAccountService.getByIdCache(userId);
//            userAccount.setIsEnabled(BusinessConstant.NO);
//            userAccountService.updateById(userAccount);
//        }
    }

    private String queryUserKey(Long userId) {
        return new SimpleDateFormat("yyyy-MM-dd").format(new Date()) + userId;
    }
}
